package com.zhangtai.mq.listener;

import com.rabbitmq.client.Channel;
//import com.zhangtai.Neusoft.service.NeuEmployeeBaseInfoService;
import com.zhangtai.exception.RException;
import com.zhangtai.exception.SendErr;
import com.zhangtai.modules.entity.employee.EmployeeInfoEntity;
import com.zhangtai.modules.service.employee.EmployeeInfoService;
import com.zhangtai.utils.DateUtils;
import com.zhangtai.utils.R;
import com.zhangtai.utils.StringUtils;
import com.zhangtai.utils.TypeCastUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;

@Component
@Slf4j
public class GetUserCodeListener {
    @Autowired
    private SendErr sendErr;
//    @Autowired
//    private NeuEmployeeBaseInfoService neuEmployeeBaseInfoService;
    @Autowired
    private EmployeeInfoService employeeInfoService;

//    @RabbitListener(queues = "GET_USER_CODE_QUEUE") //测试队列队列
    public void rabbitMqListener(Channel channel, Message message) {

        String s = new String(message.getBody(), StandardCharsets.UTF_8);
        log.info("更新员工工号监听的消息是:{}", s);
//        如果消息为空，则签收
        if (StringUtils.isEmpty(s)){
            try {
                channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            } catch (IOException e) {
                e.printStackTrace();

            }
        }

        try {
            Long oid = TypeCastUtils.toLong(s);  //获取oid
//            R<String> userCode = getUserCode(oid);
//            if (userCode.getFlag()){
//                //更新成功则签收
//                try {
//                    channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//                log.info("更新工号消息被签收了==========》》》》");
//
//            }


        } catch (Exception e) {
            log.error("mq消费者报错\n"+ Arrays.asList(e.getStackTrace()));
            log.error("请求报文是：{}",s);
            String time = DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN);
            ArrayList<StackTraceElement> stackTraceElements = new ArrayList<>();
            for (StackTraceElement stackTraceElement : e.getStackTrace()) {
                if (stackTraceElement.toString().contains("com.zhangtai")){
                    stackTraceElements.add(stackTraceElement);
                }
            }
            sendErr.sendMsg(time+",mq消费者报错:"+e+"\n"+ stackTraceElements);


            throw new RException("mq消费者报错",e);
        }

    }
//
//    public R<String> getUserCode(Long oid){
//        R<String> userCode = neuEmployeeBaseInfoService.getUserCode();
//        if(userCode.getFlag()){
//            EmployeeInfoEntity employeeInfoEntity = new EmployeeInfoEntity();
//            employeeInfoEntity.setOid(oid);
//            employeeInfoEntity.setCode(userCode.getData());
//            employeeInfoService.updateByIdE(employeeInfoEntity);
//        }
//        return userCode;
//    }
}
